#!/usr/bin/env bash
if [ -z "$testsdir" ]; then
  echo "Incorrect usage. You probably want ./start-test-stop help"
  exit 1
fi

PATH=.:$PATH:/usr/sbin
MAKE=${MAKE:-make}

if [ -z "$PDNS_BUILD_PATH" ]; then
  # PDNS_BUILD_PATH is unset or empty. Assume an autotools build.
  PDNS_BUILD_PATH=.

  export PDNS=${PDNS:-${PWD}/../pdns/pdns_server}
  export PDNS2=${PDNS2:-${PWD}/../pdns/pdns_server}
  export PDNSRECURSOR=${PDNSRECURSOR:-${PWD}/../pdns/recursordist/pdns_recursor}
  export RECCONTROL=${RECCONTROL:-${PWD}/../pdns/recursordist/rec_control}
  export SDIG=${SDIG:-${PWD}/../pdns/sdig}
  export NOTIFY=${NOTIFY:-${PWD}/../pdns/pdns_notify}
  export NSEC3DIG=${NSEC3DIG:-${PWD}/../pdns/nsec3dig}
  export SAXFR=${SAXFR:-${PWD}/../pdns/saxfr}
  export ZONE2SQL=${ZONE2SQL:-${PWD}/../pdns/zone2sql}
  export ZONE2JSON=${ZONE2JSON:-${PWD}/../pdns/zone2json}
  export ZONE2LDAP=${ZONE2LDAP:-${PWD}/../pdns/zone2ldap}
  export PDNSUTIL=${PDNSUTIL:-${PWD}/../pdns/pdnsutil}
  export PDNSCONTROL=${PDNSCONTROL:-${PWD}/../pdns/pdns_control}
else
  export PDNS=${PDNS:-$PDNS_BUILD_PATH/pdns-auth}
  export PDNS2=${PDNS2:-$PDNS_BUILD_PATH/pdns-auth}
  export PDNSRECURSOR=${PDNSRECURSOR:-$PDNS_BUILD_PATH/pdns/recursordist/pdns_recursor}
  export RECCONTROL=${RECCONTROL:-$PDNS_BUILD_PATH/pdns/recursordist/rec_control}
  export SDIG=${SDIG:-$PDNS_BUILD_PATH/sdig}
  export NOTIFY=${NOTIFY:-$PDNS_BUILD_PATH/pdns-auth-notify}
  export NSEC3DIG=${NSEC3DIG:-$PDNS_BUILD_PATH/nsec3dig}
  export SAXFR=${SAXFR:-$PDNS_BUILD_PATH/saxfr}
  export ZONE2SQL=${ZONE2SQL:-$PDNS_BUILD_PATH/pdns-zone2sql}
  export ZONE2JSON=${ZONE2JSON:-$PDNS_BUILD_PATH/pdns-zone2json}
  export ZONE2LDAP=${ZONE2LDAP:-$PDNS_BUILD_PATH/pdns-zone2ldap}
  export PDNSUTIL=${PDNSUTIL:-$PDNS_BUILD_PATH/pdns-auth-util}
  export PDNSCONTROL=${PDNSCONTROL:-$PDNS_BUILD_PATH/pdns-auth-control}
fi

unset _JAVA_OPTIONS

spectest=$1
[ -z $spectest ] && spectest=""

for prog in $SDIG $SAXFR $NOTIFY $NSEC3DIG; do
  if [ -z "$PDNS_BUILD_PATH" ]; then
    if `echo $prog | grep -q '\.\./pdns'`; then
      ${MAKE} -C ../pdns ${prog##*../pdns/} || exit
    fi
  else
    if [ ! -x "$prog" ]; then
      echo "Error: cannot find $prog, please build it."
      exit 1
    fi
  fi
done

export SDIG="timeout 5 $SDIG"
export NSEC3DIG="timeout 5 $NSEC3DIG"
export SAXFR="timeout 30 $SAXFR"
export PDNSCONTROL="timeout 5 $PDNSCONTROL"

rm -f test-results failed_tests passed_tests skipped_tests ${testsdir}/*/real_result ${testsdir}/*/diff ${testsdir}/*/*.out ${testsdir}/*/start ${testsdir}/*/step.*

passed=0
failed=0
skipped=0

touch passed_tests failed_tests skipped_tests

for a in $(find $testsdir -type d | grep -v ^.$ | grep -v .svn | grep -v ^confdir | LC_ALL=C sort)
do
        if [ ! -x $a/command ]
        then
                continue
        fi
        testname=$(basename $a)
        export testname
        echo  "$testname: "
        if [ "${PDNS_DEBUG}" = "YES" ]; then
                cat $a/description
        fi

        echo  "$testname: " >> test-results
        cat $a/description >> test-results

        SKIPIT=0
        if [ -e $a/skip ]
        then
                SKIPIT=1
                result="	Skipped test $a"
        else
                for reason in $skipreasons $context $backend
                do
                        if [ -e $a/skip.$reason ]
                        then
                                SKIPIT=1
                                result="	Skipped test $a for reason $reason"
                                break
                        fi
                done
        fi

        FAIL=0
        for reason in $skipreasons $context $backend
        do
                if [ -e $a/fail.$reason ]
                then
                        FAIL=1
                        break
                fi
        done

        if [ "$spectest" != "" ] && [ "$spectest" != "$testname" ] && [ "$testname" != "00dnssec-grabkeys" ]
        then
                SKIPIT=1
                result="	Skipped test $a because it's not the specified single test"
        fi


        if [ $SKIPIT = 1 ]
        then
                echo $testname >> skipped_tests
                skipped=$[$skipped+1]
        else
                $a/command > $a/real_result
                expected=$a/expected_result

                diffopts="-u"
                if [ -e $a/expected_result.i ]; then
                        expected=$a/expected_result.i
                        diffopts="${diffopts} -i"
                fi

                for extracontext in $extracontexts
                do
                        [ -e "$a/expected_result.$extracontext" ] && expected=$a/expected_result.$extracontext
                done
                [ -n "$context" ] && [ -e "$a/expected_result.$context" ] && expected=$a/expected_result.$context
                diff ${diffopts} $expected $a/real_result > $a/diff 2>&1
                if [ -s $a/diff ]
                then
                        if [ $FAIL = 0 ]
                        then
                                result="	Failed test $a"
                                echo $testname >> failed_tests
                                failed=$[$failed+1]
                                if [ "$FIX_TESTS" == "YES" ]
                                then
                                        mv -f $a/real_result $expected
                                        result="$result (fixed)"
                                fi
                        else
                                result="	Expected failure for reason $reason, test passed $a"
                                echo $testname >> passed_tests
                                passed=$[$passed+1]
                        fi
                else
                        if [ $FAIL = 0 ]
                        then
                                result="	Passed test $a"
                                echo $testname >> passed_tests
                                passed=$[$passed+1]
                        else
                                result="	Unexpected pass for reason $reason, test failed $a"
                                echo $testname >> failed_tests
                                failed=$[$failed+1]
                        fi
                fi
        fi
        echo "$result"
        echo
        echo "$result" >> test-results
        echo >> test-results
done

if [ $failed -gt 0 ]; then
        echo -n "::error title=Regression-tests::Tests failed. "
fi
echo -n $passed out of $[$passed+$failed]
echo -n " ("
res=$((echo scale=2; echo 100*$passed/\($passed+$failed\)) | bc )
echo -n "$res%) "
echo tests passed, $skipped were skipped
